package com.xzqwjw.taskmanager.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xzqwjw.taskmanager.service.DatabaseService;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AdminDao implements DatabaseService {

  private SQLiteDatabase objDB;
  private String strDBFile = "";

  public AdminDao(String strDBFile) {
    this.strDBFile = strDBFile;
  }

  @Override
  public Map<String, Object> getById(int intAID) {
    InfoAdmin objAdmin = null;
    Map<String, Object> mapInfo = null;

    String strSQL = "SELECT * FROM info_cadre_admin WHERE aid=?";
    String[] aryArgs = {String.valueOf(intAID)};

    objDB = SQLiteDatabase.openOrCreateDatabase(this.strDBFile, null);

    Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
    if (objCursor.moveToFirst()) {
      objAdmin = new InfoAdmin();
      getInfo(objCursor, objAdmin);
      mapInfo = new HashMap<String, Object>();
      mapInfo.put("UnitInfo", objAdmin);
    }
    objCursor.close();
    objDB.close();

    return mapInfo;
  }

  @Override
  public Map<String, Object> getByIdBasic(int intBasicID) {
    return null;
  }

  @Override
  public List<Map<String, Object>> listByIdBasic(int intBasicID) {
    return null;
  }

  @Override
  public List<Map<String, Object>> listAll() {
    return null;
  }

  private void getInfo(Cursor objCursor, InfoAdmin objAdmin) {
    objAdmin.AID = Integer.parseInt(objCursor.getString(objCursor.getColumnIndex("aid")));
    objAdmin.UserName = objCursor.getString(objCursor.getColumnIndex("username"));
    objAdmin.RealName = objCursor.getString(objCursor.getColumnIndex("realname"));
    objAdmin.Password = objCursor.getString(objCursor.getColumnIndex("password"));
    objAdmin.AddTime = objCursor.getString(objCursor.getColumnIndex("addtime"));
    objAdmin.LoginNumber = Integer.parseInt(objCursor.getString(objCursor.getColumnIndex("login_number")));
    objAdmin.LastLoginTime = objCursor.getString(objCursor.getColumnIndex("last_login_time"));
    objAdmin.AdminType = Integer.parseInt(objCursor.getString(objCursor.getColumnIndex("admin_type")));
  }

  public InfoAdmin CheckLogin(String strPswd) {
    InfoAdmin objAdmin = null;
    String strSQL = "SELECT * FROM info_admin WHERE username='admin' AND password=?";
    String[] aryArgs = {strPswd};
    objDB = SQLiteDatabase.openOrCreateDatabase(this.strDBFile, null);
    Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
    if (objCursor.getCount() > 0) {
      while (objCursor.moveToNext()) {
        objAdmin = new InfoAdmin();
        getInfo(objCursor, objAdmin);
      }
    } else {
      objAdmin = new InfoAdmin();
      objAdmin.AID = 0;
    }
    objCursor.close();
    objDB.close();
    return objAdmin;
  }

  @Override
  public List<Map<String, Object>> listByWhere(String strWhere) {
    return null;
  }

}
